﻿using AnesSystem.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnesSystem.Controls
{
    /// <summary>
    /// CustomItemControl.xaml 的交互逻辑
    /// </summary>
    public partial class CustomItemControl : UserControl
    {
        #region Variables

        private List<PatientVitalSignsModel> _list;

        #endregion

        #region Constructor

        public CustomItemControl()
        {
            InitializeComponent();
            _list = new List<PatientVitalSignsModel>();
        }

        #endregion

        #region Properties

        #endregion

        #region 依赖属性

        public Orientation Orientation
        {
            get { return (Orientation)GetValue(OrientationProperty); }
            set { SetValue(OrientationProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Orientation.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OrientationProperty =
            DependencyProperty.Register("Orientation", typeof(Orientation), typeof(CustomItemControl), new PropertyMetadata(Orientation.Horizontal));


        public List<FrameworkElement> ItemsSource
        {
            get { return (List<FrameworkElement>)GetValue(ItemSourceProperty); }
            set { SetValue(ItemSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ItemSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(List<FrameworkElement>), typeof(CustomItemControl), new PropertyMetadata(null, ItemSourceChangedCallBack));

        private static void ItemSourceChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CustomItemControl ctrl = d as CustomItemControl;
            if (e.NewValue == null) return;
            ctrl._list.Clear();
            foreach (var item in ctrl.ItemsSource)
            {
                ctrl._list.Add(((FrameworkElement)item).DataContext as PatientVitalSignsModel);
            }
        }



        public object SelectedItem
        {
            get { return (object)GetValue(SelectedItemProperty); }
            set { SetValue(SelectedItemProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectedItem.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedItemProperty =
            DependencyProperty.Register("SelectedItem", typeof(object), typeof(CustomItemControl), new PropertyMetadata(null));



        #endregion

        #region Event

        #endregion

        #region Private Methods

        private string FindNoEmptyValue(int start)
        {
            string str = "";
            int count = _list.Count;
            for (int i = start; i >= 0; i--)
            {
                if (!string.IsNullOrEmpty((_list[i]).ModValue))
                {
                    str = _list[i].ModValue;
                    break;
                } 
            }
            if (string.IsNullOrEmpty(str))
            {
                for (int i = start; i < count; i++)
                {
                    if (!string.IsNullOrEmpty((_list[i]).ModValue))
                    {
                        str = _list[i].ModValue;
                        break;
                    }
                }
            }
            return str;
        }

        #endregion

        #region Public/Protected Methods
        public string SerchNearValue(int startIndex)
        {
            return FindNoEmptyValue(startIndex);
        }

        #endregion

        #region EventHandlers

        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SelectedItem = sender;
        }

        #endregion
    }
}
